package com.namecard.namecardapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.SearchRecordInfo;
import com.namecard.namecardapi.mapper.SearchRecordInfoMapper;
import com.namecard.namecardapi.service.SearchRecordInfoService;
import com.namecard.namecardapi.util.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-07-07
 */
@Service
public class SearchRecordInfoServiceImpl extends ServiceImpl<SearchRecordInfoMapper, SearchRecordInfo> implements SearchRecordInfoService {
    @Resource
    private SearchRecordInfoMapper searchRecordInfoMapper;
    @Override
    public void saveSearchRecord(String searchRecord, String userId) {
        //创建保存记录模型
        SearchRecordInfo searchRecordInfo = new SearchRecordInfo();
        searchRecordInfo.setUserId(userId);
        searchRecordInfo.setContent(searchRecord);
        //保存搜索记录
        searchRecordInfoMapper.insert(searchRecordInfo);
    }

    @Override
    public List<SearchRecordInfo> getSearchRecord(String userId) {
        return searchRecordInfoMapper
                        .selectList(new QueryWrapper<SearchRecordInfo>()
                        .eq("user_id", userId)
                        .orderByDesc("create_time")
                        .last("limit " + Constants.SEARCH_RECORD_LIMIT_NUM));
    }

    @Override
    public void deleteSearchRecord(Long searchRecordId) {
        searchRecordInfoMapper.deleteById(searchRecordId.longValue());
    }

    @Override
    public void deleteAllSearchRecord(String userId) {
        searchRecordInfoMapper.delete(new QueryWrapper<SearchRecordInfo>().eq("user_id", userId));
    }
}
